/**
 * @class
 * @memberof w.parser
 * @param {*} message 消息
 * @param {*} callback 处理完成的回调
 */
function createRing(message, callback) {
    let { row, column, irad, orad } = message;
    let position = new Array(),
        normal = new Array(),
        color = new Array(),
        indices = new Array();
    for (var i = 0; i <= row; i++) {
        var r = Math.PI * 2 / row * i; //管道圆上每个分段的弧度
        var rr = Math.cos(r);
        var ry = Math.sin(r);
        for (var ii = 0; ii <= column; ii++) {
            var tr = Math.PI * 2 / column * ii;
            //每个顶点位置的x、y、z分量
            var ty = (rr * irad + orad) * Math.cos(tr);
            var tz = ry * irad;
            var tx = (rr * irad + orad) * Math.sin(tr);

            var rx = rr * Math.cos(tr);
            var rz = rr * Math.sin(tr);
            position.push(tx, ty, tz);
            normal.push(rx, ry, rz);
            var tc = hsva(360 / column * ii, 1, 1, 1);
            color.push(tc[0], tc[1], tc[2], tc[3]);
        }
    }
    for (i = 0; i < row; i++) {
        for (ii = 0; ii < column; ii++) {
            r = (column + 1) * i + ii;
            indices.push(r, r + column + 1, r + 1);
            indices.push(r + column + 1, r + column + 2, r + 1);
        }
    }
    callback({ data: { indices, position, color, normal }, transferlist: [indices.buffer, position.buffer, color.buffer, normal.buffer] });
}
function hsva(h, s, v, a) {
    if (s > 1 || v > 1 || a > 1) {
        return;
    }
    var th = h % 360;
    var i = Math.floor(th / 60);
    var f = th / 60 - i;
    var m = v * (1 - s);
    var n = v * (1 - s * f);
    var k = v * (1 - s * (1 - f));
    var color = new Array();
    if (!s > 0 && !s < 0) {
        color.push(v*255, v*255, v*255, a*255);
    } else {
        var r = new Array(v, n, m, m, k, v);
        var g = new Array(k, v, v, n, m, m);
        var b = new Array(m, m, k, v, v, n);
        color.push(r[i]*255, g[i]*255, b[i]*255, a*255);
    }
    return color;
}

export { createRing }